package com.pj.service.Impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pj.conf.RagflowConfig;
import com.pj.service.RagFlowDataSetService;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author zzy
 * @date 2025/6/20 10:10
 * @description : 类的描述
 */
@Service
public class RagFlowServiceImpl implements RagFlowDataSetService {

    private final WebClient webClient;
    private final RagflowConfig ragflowConfig;

    public RagFlowServiceImpl(WebClient.Builder webClientBuilder, RagflowConfig ragflowConfig) {
        this.webClient = webClientBuilder.baseUrl(ragflowConfig.getBaseUrl()).build();
        this.ragflowConfig = ragflowConfig;
    }

    @Override
    public List<Map<String, Object>> listDatasets(String ragFlowApi) {
        Map<String, Object> response = webClient.get()
                .uri(ragFlowApi)
                .header("Authorization", "Bearer " + ragflowConfig.getApiKey())
                .retrieve()
                .bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})
                .block();

        List<Map<String, Object>> resultList = new ArrayList<>();

        if (response != null && Integer.valueOf(response.get("code").toString()) == 0) {
            Object data = response.get("data");
            if (data != null) {
                ObjectMapper objectMapper = new ObjectMapper();
                resultList = objectMapper.convertValue(data, new TypeReference<List<Map<String, Object>>>() {});
            }
        } else if (response != null) {
            System.out.println("查询失败，错误信息：" + response.get("message"));
        } else {
            System.out.println("接口响应为空！");
        }

        return resultList;
    }

    @Override
    public List<Map<String, Object>> addDataset(String ragFlowApi, Map<String, Object> requestBody) {
        Map<String, Object> response = webClient.post()
                .uri(ragFlowApi)
                .header("Authorization", "Bearer " + ragflowConfig.getApiKey())
                .bodyValue(requestBody)
                .retrieve()
                .onStatus(status -> !status.is2xxSuccessful(), clientResponse ->
                        clientResponse.bodyToMono(String.class)
                                .flatMap(errorBody -> Mono.error(new RuntimeException("调用失败: " + errorBody)))
                )
                .bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})
                .block();

        List<Map<String, Object>> resultList = new ArrayList<>();

        if (response != null && Integer.valueOf(response.get("code").toString()) == 0) {
            Object data = response.get("data");
            if (data != null) {
                ObjectMapper objectMapper = new ObjectMapper();
                resultList = objectMapper.convertValue(data, new TypeReference<List<Map<String, Object>>>() {});
                if (!resultList.isEmpty()) {
                    System.out.println("创建成功：" + resultList.get(0).get("name"));
                }
            }
        } else if (response != null) {
            System.out.println("创建失败，错误信息：" + response.get("message"));
        } else {
            System.out.println("接口响应为空！");
        }

        return resultList;
    }



}
